package com.aw.baselib.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.aw.baselib.R;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.internal.InternalAbstract;

/**
 * @author : hzw
 */
public class MyRefreshView extends InternalAbstract {

    private static final int TAG_DEFAULT = 0;
    private static final int TAG_REFRESH_PULLDOWN = 1;
    private static final int TAG_LOADING = 2;
    private static final int TAG_RELEASE = 3;

    private ImageView mIvImgLoading, mIvImgArrowDown, mIvImgArrowUp;
    private TextView mTvRefreshTxt;
    private Animation mAnimation;
//    private AnimationDrawable animationDrawable;

    protected MyRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.layout_refresh_header, this);

        mIvImgLoading = findViewById(R.id.iv_imgLoading);
        mIvImgArrowDown = findViewById(R.id.iv_imgArrowDown);
        mIvImgArrowUp = findViewById(R.id.iv_imgArrowUp);
        mTvRefreshTxt = findViewById(R.id.tv_refreshTxt);

        // 加载动画A
        mAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
//        iv_img.setImageResource(R.drawable.refresh_anim);
//        animationDrawable = (AnimationDrawable) iv_img.getDrawable();
    }

    @Override
    public int onFinish(@NonNull RefreshLayout layout, boolean success) {
        if (success) {
            mIvImgLoading.clearAnimation();
//            animationDrawable.stop();
            mTvRefreshTxt.setText("刷新完成");
        } else {
            mIvImgLoading.clearAnimation();
//            animationDrawable.stop();
            mTvRefreshTxt.setText("刷新失败");
        }
        super.onFinish(layout, success);
        return 500; //延迟500毫秒之后再弹回
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        switch (newState) {
            case PullDownToRefresh: //下拉过程sh
                showView(TAG_REFRESH_PULLDOWN);
                break;
            case ReleaseToRefresh: //松开刷新
                showView(TAG_RELEASE);
                break;
            case Refreshing: //loading中
                showView(TAG_LOADING);
                break;
        }
    }

    private void showView(int tag) {
        switch (tag) {
            case TAG_DEFAULT:
                showView(mIvImgArrowDown, true);
                showView(mIvImgArrowUp, false);
                showView(mIvImgLoading, false);
                break;
            case TAG_REFRESH_PULLDOWN:
                showView(mIvImgArrowDown, true);
                showView(mIvImgArrowUp, false);
                showView(mIvImgLoading, false);
                mTvRefreshTxt.setText("下拉刷新");
                break;
            case TAG_RELEASE:
                showView(mIvImgArrowDown, false);
                showView(mIvImgArrowUp, true);
                showView(mIvImgLoading, false);
                mTvRefreshTxt.setText("松开刷新");
                break;
            case TAG_LOADING:
                showView(mIvImgArrowDown, false);
                showView(mIvImgArrowUp, false);
                showView(mIvImgLoading, true);
                mTvRefreshTxt.setText("正在加载");
                mIvImgLoading.startAnimation(mAnimation);
                break;
        }

    }

    private void showView(View view, boolean show) {
        if(view == null) {
            return;
        }
        view.setVisibility(show ? View.VISIBLE : View.GONE);
    }
}
